#include "VSettings.h"
#include "ui_VSettings.h"
#include <QMessageBox>
#include <QDebug>
#include "CUIHelper.h"
#include "CMainWindow.h"
#include <QFontDatabase>
#include <QListWidget>
#include <QTime>
#include <QTimer>
#include "VToast.h"
#include "CTableConfig.h"

VSettings::VSettings(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::VSettings)
{
    ui->setupUi(this);
    this->setWindowFlag(Qt::WindowContextHelpButtonHint,false);

    //异步初始化
    QTimer::singleShot(20,this,[=]{
        loadSysFont(); //获取字体 比较耗时
        readSettings();
        connect(ui->comboBox_chinese, static_cast<void(QComboBox::*)(const QString &)>(&QComboBox::currentIndexChanged),
             [=](const QString &text){
            Q_UNUSED(text)
            resetFont();
        });
        connect(ui->comboBox_english, static_cast<void(QComboBox::*)(const QString &)>(&QComboBox::currentIndexChanged),
             [=](const QString &text){
            Q_UNUSED(text)
            resetFont();
        });
        connect(ui->spinBox_font_size, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged),
              [=](int i){
            Q_UNUSED(i)
            resetFont();
        });
    });
}

VSettings::~VSettings()
{
    delete ui;
}

VSettings *VSettings::getInstance()
{
    static VSettings *pInstance = nullptr;
    if(nullptr == pInstance) {
        pInstance = new VSettings();
    }
    return pInstance;
}

void VSettings::readSettings()
{
    //首选项
    ui->spinBox_port->setValue(CTableConfig::instance()->queryValue(CTableConfig::DefaultPort).toInt());
    ui->comboBox_text_code->setCurrentText(CTableConfig::instance()->queryValue(CTableConfig::TextCode));
    ui->spinBox_maxline->setValue(CTableConfig::instance()->queryValue(CTableConfig::MaxLine).toInt());
    ui->spinBox_span->setValue(CTableConfig::instance()->queryValue(CTableConfig::Span).toInt());

    QString strLanguage = CTableConfig::instance()->queryValue(CTableConfig::Language);
    if("简体中文" == strLanguage) {
        ui->radioButton_zn->setChecked(true);
    } else if("English" == strLanguage) {
        ui->radioButton_english->setChecked(true);
    }

    //外观
    QStringList strListFont = CTableConfig::instance()->queryValue(CTableConfig::Font).split(',');
    if(!strListFont.isEmpty()){
        ui->comboBox_english->setCurrentText(strListFont.first());
        ui->comboBox_chinese->setCurrentText(strListFont.last());
    }
    ui->spinBox_font_size->setValue(CTableConfig::instance()->queryValue(CTableConfig::FontSize).toInt());
}

//void VSettings::writeSettings()
//{
//    CTableConfig::instance()->updateValue(CTableConfig::DefaultPort,ui->spinBox_port->text());
//    CTableConfig::instance()->updateValue(CTableConfig::TextCode,ui->comboBox_text_code->currentText());
//    CTableConfig::instance()->updateValue(CTableConfig::MaxLine,ui->spinBox_maxline->text());
//    CTableConfig::instance()->updateValue(CTableConfig::Span,ui->spinBox_span->text());

//    if(ui->radioButton_english->isChecked()){
//        CTableConfig::instance()->updateValue(CTableConfig::Language,"English");
//    }else{
//        CTableConfig::instance()->updateValue(CTableConfig::Language,"简体中文");
//    }
//}

void VSettings::loadSysFont()
{
    QStringList strErrorFont; //不可用的字体
    strErrorFont<<"Fixedsys"<<"Terminal"<<"MS Sans Serif"<<"MS Serif"<<"Small Fonts"<<"System";
    QFontDatabase fontData;
    //中文字体选择框
//    qDebug()<<"11111111 "<<QTime::currentTime();
    QListWidget *pListWidget = new QListWidget(this);
    foreach(QString strFont,fontData.families(QFontDatabase::SimplifiedChinese)){
        if(strErrorFont.contains(strFont))
            continue ;
        QListWidgetItem *pItem = new QListWidgetItem(strFont,pListWidget);
        pItem->setFont(QFont(strFont));
        pListWidget->addItem(pItem);
    }
//    qDebug()<<"11111111 2"<<QTime::currentTime();
    ui->comboBox_chinese->setModel(pListWidget->model());
    ui->comboBox_chinese->setView(pListWidget);
//    qDebug()<<"11111111 3"<<QTime::currentTime();


    //英文字体选择框
//    qDebug()<<"222222222 1"<<QTime::currentTime();
    QListWidget *pListWidgetE = new QListWidget(this);
    foreach(QString strFont,fontData.families(QFontDatabase::Latin)){
        if(strErrorFont.contains(strFont))
            continue ;
        QListWidgetItem *pItem = new QListWidgetItem(strFont,pListWidgetE);
        pItem->setFont(QFont(strFont));
        pListWidgetE->addItem(pItem);
    }
//    qDebug()<<"222222222 2"<<QTime::currentTime();
    ui->comboBox_english->setModel(pListWidgetE->model());
    ui->comboBox_english->setView(pListWidgetE);
//    qDebug()<<"222222222 1"<<QTime::currentTime();
}

void VSettings::closeEvent(QCloseEvent *)
{
    hide();
}

void VSettings::resetFont()
{
    //多个字体 首先会在第一个字体中寻找 依次往后
    QFont font = QApplication::font();
    font.setFamily(QString("%1,%2").arg(ui->comboBox_english->currentText())
                   .arg(ui->comboBox_chinese->currentText()));
    font.setPixelSize(ui->spinBox_font_size->value());
//    QApplication::setFont(QFont(),"QObject");
    QApplication::setFont(font,"QObject");
    qInfo()<<"重置字体:"<<font.family()<<font.pixelSize();


    CTableConfig::instance()->updateValue(CTableConfig::Font,QString("%1,%2")
                         .arg(ui->comboBox_english->currentText())
                         .arg(ui->comboBox_chinese->currentText()));
    CTableConfig::instance()->updateValue(CTableConfig::FontSize,ui->spinBox_font_size->text());
}

void VSettings::on_radioButton_zn_clicked(bool checked)
{
    Q_UNUSED(checked)
    CTableConfig::instance()->updateValue(CTableConfig::Language,"简体中文");
    VToast::getInstance()->show(tr("重启生效"));
}

void VSettings::on_radioButton_english_clicked(bool checked)
{
    Q_UNUSED(checked)
    CTableConfig::instance()->updateValue(CTableConfig::Language,"English");
    VToast::getInstance()->show(tr("重启生效"));
}

void VSettings::on_spinBox_port_valueChanged(int arg1)
{
    CTableConfig::instance()->updateValue(CTableConfig::DefaultPort,QString::number(arg1));
}

void VSettings::on_comboBox_text_code_currentTextChanged(const QString &arg1)
{
    CTableConfig::instance()->updateValue(CTableConfig::TextCode,arg1);
    CMainWindow::getInstance()->slotTextCode(arg1);
}

void VSettings::on_spinBox_maxline_valueChanged(int arg1)
{
    CTableConfig::instance()->updateValue(CTableConfig::MaxLine,QString::number(arg1));
}

void VSettings::on_spinBox_span_valueChanged(int arg1)
{
    CTableConfig::instance()->updateValue(CTableConfig::Span,QString::number(arg1));
}
